package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品单位Controller
 */
@Tag(name = "商品单位接口管理")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {
    @Autowired
    private IProductUnitService productUnitService;

    /**
     * 获取分页列表
     * @param productUnit
     * @return
     */
    @Operation(summary = "获取分页列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductUnit productUnit){
        startPage();
        List<ProductUnit> productUnitList = productUnitService.selectProductUnitPage(productUnit);
        return getDataTable(productUnitList);
    }
    /**
     * 获取商品单位详细信息
     */
    @Operation(summary = "获取商品单位详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(productUnitService.selectProductUnitById(id));
    }

    /**
     * 新增商品单位
     */
    @Operation(summary = "新增商品单位")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ProductUnit productUnit){
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.insertProductUnit(productUnit));
    }

    /**
     * 修改商品单位
     */
    @Operation(summary = "修改商品单位")
    @PutMapping
    public AjaxResult changeProductUnit(@RequestBody @Validated ProductUnit productUnit) {
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateProductUnit(productUnit));
    }

    /**
     * 删除商品单位
     */
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult removeProductUnits(@PathVariable Long[] ids){
        return toAjax(productUnitService.deleteProductUnitByIds(ids));
    }

    /**
     * 新增商品时加载商品单位数据
     * 获取全部单位
     * @return
     */
    @Operation(summary = "获取全部单位")
    @GetMapping("getUnitAll")
    public AjaxResult selectProductUnitAll(){
        return success(productUnitService.list());
    }
}
